﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using KgUtils.Resources;
using KgUtils.Collections;

namespace KgUtils.Web
{
    public static partial class FormHelper
    {
        public static void SetYesOrNoFromBool(DropDownList ddl, bool? value)
        {
            if (value.HasValue)
            {
                if (value.Value)
                    ddl.Text = Labels.Yes;
                else
                    ddl.Text = Labels.No;
            }
            else
                ddl.Text = String.Empty;
        }

        public static bool? GetBoolFromYesOrNo(DropDownList ddl)
        {
            if (ddl.Text != String.Empty)
            {
                if (ddl.Text == Labels.Yes)
                    return true;
                else
                    return false;
            }
            else
                return null;
        }

        public static void SetYesOrNoFromString(DropDownList ddl, string value)
        {
            if (value != null)
            {
                if (value == String.Empty)
                    ddl.Text = Labels.No;
                else
                    ddl.Text = Labels.Yes;
            }
            else
                ddl.Text = String.Empty;
        }

        public static string GetStringFromYesOrNo(DropDownList ddl, string yesValue)
        {
            if (ddl.Text != String.Empty)
            {
                if (ddl.Text == Labels.Yes)
                    return yesValue;
                else
                    return String.Empty;
            }
            else
                return null;
        }

        public static void SetYesNoDataSource(DropDownList ddl)
        {
            var ds = new List<string> { Labels.Yes, Labels.No };
            ds.Insert(0, String.Empty);

            ddl.DataSource = ds;
            ddl.DataBind();
        }

        public static void SetListItemsFromString(ListControl listControl, string values)
        {
            foreach (ListItem item in listControl.Items)
                item.Selected = false;

            if (!values.IsNullOrEmpty())
            {
                var itemsToCheck = values.Split(';');
                foreach (var item in itemsToCheck)
                    listControl.Items.FindByValue(item).Selected = true;
            }
        }

        public static string GetStringFromListItems(ListControl listControl)
        {
            List<string> selectedItems = new List<string>();
            foreach (ListItem item in listControl.Items)
            {
                if (item.Selected)
                    selectedItems.Add(item.Value);
            }
            return String.Join(";", selectedItems.ToArray());
        }

        public static void SetListItemsFromList(ListControl listControl, List<string> values)
        {
            foreach (ListItem item in listControl.Items)
                item.Selected = false;

            if (values != null)
            {
                foreach (var item in values)
                    listControl.Items.FindByValue(item).Selected = true;
            }
        }

        public static List<string> GetListFromListItems(ListControl listControl)
        {
            List<string> selectedItems = new List<string>();
            foreach (ListItem item in listControl.Items)
            {
                if (item.Selected)
                    selectedItems.Add(item.Value);
            }
            return selectedItems;
        }
    }
}
